import React, {useContext, useEffect, useState} from 'react';
import {StatusBar, ImageBackground, View, TextInput, Dimensions} from 'react-native';
import { Button, Image, Text } from 'react-native-elements';

import LocaleContext from '../libs/locale';
import AsyncStorage from "@react-native-async-storage/async-storage";
import WalletContext from "../libs/wallet";
import {TouchableHighlight} from "react-native-gesture-handler";
import Toast from "react-native-simple-toast";
import {constansts} from "../libs/constansts";
import {bytes2Hex, keyPairFromSeed, randomSeed, toAddress} from "../libs/keyPair";
import {convertKeyPair} from "../libs/ed2curve";
import {enAES, joinGroup} from "../libs/chatGroup";

const {width,height,scale} = Dimensions.get("window");

const ChatGroupJoin = ({ navigation,route }: any) => {
    const locale:any = useContext(LocaleContext);
    const wallet: any = useContext(WalletContext);

    const[ChatGroupName,setChatGroupName] = useState("")

    const joinChatGroup = async () => {
        let data = JSON.parse(ChatGroupName);



        let myChatGroupList:any = [];
        let myChatGroupListStr = await AsyncStorage.getItem("myChatGroupList");
        if (myChatGroupListStr) {
            myChatGroupList = JSON.parse(myChatGroupListStr);
        }
        let res_Index =myChatGroupList.findIndex((item:any)=> item.addressOut===data.addressOut);
        if(res_Index > -1)
        {
            if(myChatGroupList[res_Index].show == false)
            {
                myChatGroupList[res_Index].show =true;
                await AsyncStorage.setItem("myChatGroupList",JSON.stringify(myChatGroupList));
                navigation.replace("ChatGroupView",{Q_Address:data.addressOut,Q_Key:data.Key,QP_Priv:data.Voucher,Creator:data.Creator,desc:data.desc});
            }
            Toast.show("您已经加入该群");
            return;
        }
        myChatGroupList.push(data);
        await joinGroup(wallet.address,data.Creator,data.desc,enAES(wallet.seed,data.Key),data.addressOut,data.Voucher)
        await AsyncStorage.setItem("myChatGroupList",JSON.stringify(myChatGroupList));
        navigation.navigate("ChatGroupView",{Q_Address:data.addressOut,Q_Key:data.Key,QP_Priv:data.Voucher,Creator:data.Creator,desc:data.desc});
    }

    return (
        <>
            <TextInput
                // underlineColorAndroid={"black"}
                // errorMessage={}

                // value={ChatNum}
                onChangeText={async (value) => {
                    // console.log(value)
                    setChatGroupName(value);
                    if(value == "")
                    {
                        Toast.show("请输入群信息")
                        return ;
                    }
                    console.log("1")
                }}
                placeholder={"请输入群信息"}

                style={{marginTop:5,fontSize: 20,height:50,width:width,padding:0,paddingLeft:width*0.05,borderBottomWidth:1,borderBottomColor:"#F3F3F3",color:"black",fontWeight:"bold"}}
            />
            <TouchableHighlight
                underlayColor={ChatGroupName==""?"#A3F2FE":"#08A9D0"}
                style={{margin:25,padding:10,backgroundColor:ChatGroupName==""?"#A3F2FE":"#02CEFF",alignItems:"center",borderRadius:15}}
                disabled={ChatGroupName==""}
                onPress={async () => {
                    if(ChatGroupName!=="")
                    {
                        await joinChatGroup();
                    }
                }}>
                <Text style={{fontSize:18,color:"#fff"}}>加入群聊</Text>
            </TouchableHighlight>

        </>
    );
};

export default ChatGroupJoin;
